package Day016_字符流_Properties类.ZuoYe;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author zhangyan
 * @date 2020/5/8 15:27
 */
public class Test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //创建学生对象集合
        ArrayList<Student> list = new ArrayList<>();
        //循环输入
        for (int i = 1; i <= 3; i++) {
            System.out.println("输入第" + i + "个学生的姓名");
            String name = sc.next();
            System.out.println("输入第" + i + "个学生的年龄");
            String age = sc.next();
            System.out.println("输入第" + i + "个学生的性别");
            String sex = sc.next();
            System.out.println("输入第" + i + "个学生的学号");
            String ID = sc.next();
            //将信息保存至学生对象
            Student stu = new Student(name, age, sex, ID);
            //将学生对象添加到学生集合
            list.add(stu);
        }
        //创建字符输出流对象
        //捕获异常
        try (FileWriter fw = new FileWriter("Demo02\\aaa\\Student.txt")){
            //循环遍历集合
            for (int i = 0; i < list.size(); i++) {
                Student s = list.get(i);
                //获取每个学生信息的属性
                String s1 = s.getID() + "," + s.getName() + "," + s.getAge() + "," + s.getSex();
                fw.write(s1 + "\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

class Student {
    String name;
    String age;
    String sex;
    String ID;

    public Student() {
    }

    public Student(String name, String age, String sex, String ID) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                ", ID='" + ID + '\'' +
                '}';
    }
}
